﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Linq;

using ESF.General;
using ESF.WinForms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Windows.Media.Imaging;
using System.Threading;
using System.Threading.Tasks;
using System.Globalization;
using System.Data;
using ESF.Data;
using System.Windows.Media;
using ESF.FileProcessing;
using ESF.WinForms.Elements;
using ESF.General.GUI;

#endregion


namespace ESF.FileHelper.Controls
{
	public partial class uscFileSizeOptimizer : BaseUserControl
	{
		#region Eventos

		public event Program.DelegateReturnMenu ReturningMenu;

		#endregion


		#region Miembros

		FileSizeOptimizer _FileSizeOptimizer;

		#endregion


		#region Constructor
		
		public uscFileSizeOptimizer() : base ()
		{
			InitializeComponent();

			ListLoader.LoadList (ddlMaxSizeImageByPage, "1600x1600", "1600x1600", "1300x1300", "1300x1300", "1100x1100", "1100x1100");
			ListLoader.RemoveSelectItem (ddlMaxSizeImageByPage);

			_FileSizeOptimizer = new FileSizeOptimizer(ReportProgress);
		}

		#endregion


		#region Funciones

		private void ReportProgress(string pMessage = "", int pTotalWorkCount = 0, int pReadyWorkCount = 0, int pTotalErrors = 0, long pItemElapsedMs = 0)
		{
			GUIHelper.SetAdvance(ProgressDialog, pMessage, pTotalWorkCount, pReadyWorkCount, pTotalErrors, pItemElapsedMs);
		}

		#endregion


		#region Link Events

		private void lnkSelectFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (fbdSelectFolder.ShowDialog (this) == DialogResult.OK)
			{
				txtFolder.Text = fbdSelectFolder.SelectedPath;
				return;
			}

			txtFolder.Text = string.Empty;
		}


		private void lnkSetTempWorkFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (fbdSelectFolder.ShowDialog(this) == DialogResult.OK)
			{
				txtTmpWorkFolder.Text = fbdSelectFolder.SelectedPath;
				return;
			}

			txtTmpWorkFolder.Text = string.Empty;
		}

		#endregion


		#region Button Events

		private void btnReturnMenu_Click(object sender, EventArgs e)
		{
			if (ReturningMenu != null)
			{
				ReturningMenu ();
			}
		}
		
		
		private void btnOptimizeFiles_Click(object sender, EventArgs e)
		{
			int width;
			int height;

			string [] wh;

			string [] validExtensions = new string [] { ".PDF", ".JPG", ".JPEG", ".BMP", ".TIF" };


			if (TextHelper.IsEmpty (txtIndexName.Text))
			{
				MessageBox.Show (this, "Diligencie el nombre del indice", "Parametros Invalidos", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return;
			}

			if (!Directory.Exists (txtTmpWorkFolder.Text))
			{
				MessageBox.Show (this, "Diligencie una ruta valida para la carpeta temporal de trabajo", "Parametros Invalidos", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return;
			}

			btnOptimizeFiles.Enabled = false;
			
			ProgressDialog.HideWhenComplete = false;
			ProgressDialog.Show (ParentForm);
			ProgressDialog.Clear ();
			ProgressDialog.IsCancelable = true;

			try
			{
				ESF.General.FileHelper.EnsureFolder (txtTmpWorkFolder.Text);


				wh = ListLoader.GetSelectedValueBasicReference (ddlMaxSizeImageByPage).Name.Split ('x');

				width = int.Parse (wh [0]);
				height = int.Parse (wh [1]);

				_FileSizeOptimizer.Initialize (width, height,
					chkResizeImages.Checked, txtTmpWorkFolder.Text, 
					chkTitle.Checked, txtTitle.Text, 
					chkAuthor.Checked, txtAuthor.Text);

				Task.Factory.StartNew (delegate ()
				{
					try
					{
						_FileSizeOptimizer.OptimizeFiles (txtIndexName.Text, chkForgetAdvance.Checked,
							chkVerifyFileList.Checked, txtFolder.Text, validExtensions);
					}
					catch (Exception pEx)
					{
						LogError (pEx);
					}
					finally
					{
						WinFormsHelper.SetEnabled (btnOptimizeFiles, true);
					}
				});
			}
			catch (Exception pex)
			{
				LogError (pex);

				btnOptimizeFiles.Enabled = true;
			}
		}


		protected override void CancelCurrentOperation()
		{
			btnOptimizeFiles.Enabled = true;

			_FileSizeOptimizer.Cancel();
		}

		#endregion


		#region CheckBox Events

		private void chkTitle_CheckedChanged(object sender, EventArgs e)
		{
			txtTitle.Enabled = chkTitle.Checked;
			txtTitle.Focus ();
		}

		private void chkAuthor_CheckedChanged(object sender, EventArgs e)
		{
			txtAuthor.Enabled = chkAuthor.Checked;
			txtAuthor.Focus ();
		}

		private void chkCompressPDFImages_CheckedChanged(object sender, EventArgs e)
		{
			ddlMaxSizeImageByPage.Enabled = chkResizeImages.Checked;
		}

		#endregion
	}
}
